package com.easy.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.diagnosis.DiagnosisUtils;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.ExtendParams;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.domain.BusinessParams;
import com.alipay.api.request.AlipayTradePrecreateRequest;

import com.alipay.api.FileItem;
import com.easy.bean.Order;

import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

public class PaymentUtil {
    //public static void main(String[] args) throws AlipayApiException {



    public static String payment(Order order) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();




        // 设置商户订单号
        /*model.setOutTradeNo("20150320010101001");*/
        model.setOutTradeNo(String.valueOf(order.getOrderid()));//String.valueOf(order.getOrderid())

        // 设置订单总金额
        model.setTotalAmount(order.getMoney().toString());//order.getMoney().toString()

        // 设置订单标题
        model.setSubject("水果商城");

        // 设置产品码
        model.setProductCode("FACE_TO_FACE_PAYMENT");

        // 设置卖家支付宝用户ID
        model.setSellerId("2088721061087583");

        // 设置订单附加信息
        /*model.setBody("Iphone6 16G");*/

        // 设置订单包含的商品列表信息
//        List<GoodsDetail> goodsDetail = new ArrayList<GoodsDetail>();
//        GoodsDetail goodsDetail0 = new GoodsDetail();
//        goodsDetail0.setGoodsName("ipad");
//        goodsDetail0.setQuantity(1L);
//        goodsDetail0.setPrice("2000");
//        goodsDetail0.setGoodsId("apple-01");
//        goodsDetail0.setGoodsCategory("34543238");
//        goodsDetail0.setCategoriesTree("124868003|126232002|126252004");
//        goodsDetail0.setShowUrl("http://www.alipay.com/xxx.jpg");
//        goodsDetail.add(goodsDetail0);
//        model.setGoodsDetail(goodsDetail);
        // 设置业务扩展参数
       /* ExtendParams extendParams = new ExtendParams();
        extendParams.setSysServiceProviderId("2088511833207846");
        model.setExtendParams(extendParams);*/

        // 设置商户传入业务信息
//        BusinessParams businessParams = new BusinessParams();
//        businessParams.setMcCreateTradeIp("127.0.0.1");
//        model.setBusinessParams(businessParams);

        // 设置可打折金额
//        model.setDiscountableAmount("80.00");

        // 设置商户门店编号
//        model.setStoreId("NJ_001");

        // 设置商户操作员编号
//        model.setOperatorId("yx_001");

        // 设置商户机具终端编号
//        model.setTerminalId("NJ_T_001");

        // 设置商户的原始订单号
//        model.setMerchantOrderNo("20161008001");

        request.setBizModel(model);
        request.setNotifyUrl("https://2d32b5a8.r10.cpolar.top/order/paymentsuccess");
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradePrecreateResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            String body=response.getBody();
            JSONObject obj=JSON.parseObject(body);
            obj=obj.getJSONObject("alipay_trade_precreate_response");
            String qr_code=obj.getString("qr_code");
            System.out.println(qr_code);
            System.out.println("调用成功");
            return qr_code;
        } else {
            System.out.println("调用失败");
            String diagnosisUrl= DiagnosisUtils.getDiagnosisUrl(response);
            System.out.println(diagnosisUrl);
            return null;
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }

    private static AlipayConfig getAlipayConfig() {
        String privateKey  = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDDAGV4CvroI44j8e1sDMvdN3ZPG8Lbxw9zAwXqs8sN7qphhL4KQNLVv3YHtODN1thvUkDwkVMu3dzi5Halc2ezbHgUCjMvYRDtgTDuTDrhHqYqk/nqrq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IDj3P8BFEZwmxRENy9PvVh1BQL7IWdAoGAKkYdTJ+dIiOcU4QRniTEo29XdiBRBxhv9oBpj/jBj3NP5+TlMrqp8y+aQOKXqNWasNF3EghMryB6G4FsRMinT0W9xV4dRtr2ubPxBWg2mTRft+gdkeCpoibbreRO/2jW3HNColdg1/BVvfOkRXlUm92Up6Xeupu5hPg/sgbD9dkCgYEAudOO9p9p7rfZVGUKTPYJPlKSddYO1mCM1lNQsEqwDMIB1Sm59dTcjRon+FaokL/DEACLUHQuer9lcjlCCCqilJ7/4HiTX1Xgbt9eeXFwL88AOaCFNVfc9VZbg40upnHaFa4othags1nb/OxbszUjiIj6NfMac31I2ZCiFD3gb10=";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAooh9sYp+1J7stJziEKfXMm4dRbTbuRcALgamC6xE3wmZJ0Wq77oHRU5UANWKf6TY/maA1g9t5X1jqUCqCBbGHY2kCq0RFfealycqPuWvlPGbtdzkoMJECXgu7ExMG3MgQs823XLxz31JEGqgSrOj/uzgeXslqob260Y9JSibKzrMQEyqxh+BbrgXIyj3sJDpgRbPk/1CmOPJmFpQzTkEBCwPTgOHjfKnUfSk0+5auYagHoM7+w++gtk0wyM0HASJDE54He3IwUIJNJx3Ka8UWBZiQtI4cHMwye+A6UmGdwKVh/vQekF0Q686Cz5OK8qbBOEeH5AarPxbRCOpyPnv9wIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000146600867");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }
}